package com.qf.controller;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Address;
import com.qf.pojo.vo.Business;
import com.qf.service.BusinessService;
import com.qf.utils.UplodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;

@RestController
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    BusinessService businessService;
    @Autowired
    UplodUtils uplodUtils;
   @RequestMapping("/findAll")
   public BaseResp findAll(@RequestBody Address address){
        return  businessService.findAll(address);
   }
   @RequestMapping("/findByType")
    public BaseResp findByType(@RequestBody Map map){
       Integer tid = (Integer)map.get("tid");
       Integer sid =  (Integer)map.get("sid");
       Double x1 = (Double)map.get("x1");
       Double y1 = (Double)map.get("y1");
       Address address = new Address();
       address.setX1(x1);
       address.setY1(y1);
       if(tid==0){
           if(sid==0){
               return businessService.findAll(address);
           }else if(sid==1){
               return businessService.bsort(address);
           }else{
               return businessService.findAll(address);
           }
       }else{
           if(sid==0){
               return businessService.findAll(address);
           }else if(sid==1){
               return businessService.bsort(address);
           }else{
               return businessService.findAll(address);
           }
       }
   }
   @RequestMapping("/upload")
   public String upload(MultipartFile file){
       String upload = uplodUtils.upload(file);
       System.out.println("upload = " + upload);
       return upload;
   }
   @RequestMapping("/update")
   public BaseResp update(@RequestBody Business business){
       return businessService.update(business);
   }
   @RequestMapping("/caculate")
    public BaseResp caculate(@RequestBody Address address){
       System.out.println("address=======" + address);
       return businessService.caculate(address);
   }
   @RequestMapping("/findOne")
   public BaseResp findOne(@RequestBody Map map){
       String sid = map.get("id").toString();
       Integer id = Integer.valueOf(sid);
       System.out.println("id = " + id);
       return businessService.findOne(id);
   }
   @RequestMapping("/getEvaluation")
   public BaseResp getEvaluation(@RequestBody Business business){
       return businessService.getEvaluation(business);
   }
   @RequestMapping("/search")
   public BaseResp search(@RequestBody Map map){
       String sv = map.get("sv").toString();
       Double x1 = Double.valueOf(map.get("x1").toString());
       Double y1 = Double.valueOf(map.get("y1").toString());
       Address address = new Address();
       address.setX1(x1);
       address.setY1(y1);
       return businessService.search(sv,address);
   }
   @RequestMapping("/login")
   public BaseResp login(@RequestBody Map map){
       Business business = new Business();
       business.setPhone(map.get("phone").toString());
       business.setPassword(map.get("password").toString());
       return businessService.login(business);
   }
   @RequestMapping("/findByPhone")
   public BaseResp findByPhone(@RequestBody Map map){
       String phone = map.get("phone").toString();
       return businessService.findByPhone(phone);
   }

   @RequestMapping("/bsort")
    public BaseResp bsort(@RequestBody Map map){
       Integer tid = (Integer)map.get("tid");
       Integer sid =  (Integer)map.get("sid");
       Double x1 = (Double)map.get("x1");
       Double y1 = (Double)map.get("y1");
       Address address = new Address();
       address.setX1(x1);
       address.setY1(y1);
       if(tid==0){
           if(sid==0){
               return businessService.findAll(address);
           }else if(sid==1){
               return businessService.bsort(address);
           }else{
               return businessService.findAll(address);
           }
       }else{
           if(sid==0){
               return businessService.findByType(tid,address);
           }else if(sid==1){
               return businessService.btsort(tid,address);
           }else{
               return businessService.findByType(tid,address);
           }
       }
   }
}
